package cn.edu.csust.tysp.backend.service.impl;

import cn.edu.csust.tysp.backend.common.ApiPageResult;
import cn.edu.csust.tysp.backend.controller.request.SheetMusicPageQueryRequest;
import cn.edu.csust.tysp.backend.domain.SheetMusic;
import cn.edu.csust.tysp.backend.repository.SheetMusicRepository;
import cn.edu.csust.tysp.backend.repository.entity.SheetMusicEntity;
import cn.edu.csust.tysp.backend.service.SheetMusicService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class SheetMusicServiceImpl implements SheetMusicService {

    @Autowired
    SheetMusicRepository sheetMusicRepository;

    @Override
    public ApiPageResult<List<SheetMusic>> pageQuery(SheetMusicPageQueryRequest request) {
        //define specification
        Specification<SheetMusicEntity> specification = new Specification<>() {
            /**
             * construct assert
             * @param root
             * @param query
             * @param cb
             * @return
             */
            @Override
            public Predicate toPredicate(Root<SheetMusicEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                if(StringUtils.isNotBlank(request.getName())){
                    Predicate likeName = cb.like(root.get("realname").as(String.class),"%"+request.getName()+"%");
                    predicates.add(likeName);
                }
                return cb.and(predicates.toArray(new Predicate[0]));
            }
        };
        Sort sort;
        if (StringUtils.isNotBlank(request.getOrder())){
            sort = Sort.by(Sort.Direction.DESC,request.getOrder());
        }else{
            sort = Sort.by(Sort.Direction.DESC,"id");
        }
        Pageable pageable = PageRequest.of(request.getPageNo()-1,request.getPageSize(),sort);
        //查询
        Page<SheetMusicEntity> page = this.sheetMusicRepository.findAll(specification,pageable);
        List<SheetMusic> sheetMusics = new ArrayList<>(20);
        page.getContent().stream().forEach(x->{
            SheetMusic sheetMusic = new SheetMusic();
            BeanUtils.copyProperties(x,sheetMusic);
            sheetMusics.add(sheetMusic);
            });
        return ApiPageResult.ok(page.getTotalPages(),page.getTotalElements(),request.getPageSize(), (long) request.getPageNo(),sheetMusics);
    }
}
